/**
 * 北谷信息科技（广州）有限公司拥有本软件所有版权 © 2022，并保留所有权利。
 * Copyright © 2022, North Valley Information Technology (Guangzhou) Company
 * Limited, All Rights Reserved.
 */

package main.com.scrapy;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

/**
 * TODO: 给出类和接口说明
 *
 * @author Double
 * <p><b>Creation Time:</b> 2022/9/20 15:42</p>
 * @since scrapy-1688
 */
public class JsReaderTest {

  public static void main(String[] args) throws Exception {

    ScriptEngineManager maneger = new ScriptEngineManager();
    ScriptEngine engine = maneger.getEngineByName("JavaScript");
    System.out.println("文件路径" + System.getProperty("user.dir"));
    String projectDir = System.getProperty("user.dir");
    //    File file = new File(projectDir + "/script/createSign.js");
    File file = new File(projectDir + "/script/getProductData.js");
    FileInputStream fileInputStream = new FileInputStream(file);
    Reader scriptReader = new InputStreamReader(fileInputStream, "utf-8");
    engine.eval(scriptReader);
    if (engine instanceof Invocable) {
      // n.token + "&" + s + "&" + a + "&" + o.data
      // data: {"dataType":"moduleData","argString":"{\"memberId\":\"b2b-2599038082431ab\",\"appName\":\"pcmodules\",\"resourceName\":\"wpOfferColumn\",\"type\":\"view\",\"version\":\"1.0.0\",\"appdata\":{\"sortType\":\"wangpu_score\",\"sellerRecommendFilter\":false,\"mixFilter\":false,\"tradenumFilter\":false,\"quantityBegin\":null,\"pageNum\":1,\"count\":30}}"}
      // appKey: 12574478
      // 调用JS方法
      String token = "c1d21ba737a1abd7c2cf2b1c03e49d68";
      long s = System.currentTimeMillis();
      String a = "12574478";
      String data = "";
      String param = token.concat("&").concat(String.valueOf(s)).concat("&").concat(a).concat("&")
          .concat(data);

      Invocable invocable = (Invocable) engine;
      //      String result = (String) invocable.invokeFunction("createSign", new Object[]{param});
      String result = (String) invocable.invokeFunction("s", new Object[]{"_m_h5_tk"});
      System.out.println(result);
      System.out.println(result.length());

    }
  }
}
